<?php
/**
 * Created by PhpStorm.
 * User: 会吃曹德鱼
 * Date: 16/8/5
 * Time: 10:18
 */

namespace Recruit\Api;


use Common\Api\Api;
use Recruit\Model\ProjectModel;

class ProjectApi extends Api{
    /*
     * 添加
     * */
    const ADD = 'Recruit/Project/add';

    /*
     * 分页查询所有数据
     * */
    const QUERY = 'Recruit/Project/query';

    /*
     * 查询一条数据
     * */
    const GET_INFO = 'Recruit/Project/getinfo';
    /*
     * 根据Id保存
     * */
    const SAVEBYID = 'Recruit/Project/saveByID';
    /*
     * 真实删除数据
     * */
    const DELETE = 'Recruit/Project/delete';

    /*
     * 连表查询 连接project_apply
     * */
    const QUERY_JOIN = 'Recruit/Project/queryjoin';
    /*
    * 三表连接查询
    * */
    const QUERY_THREE_JOIN = 'Recruit/Project/querythreejoin';

    protected function _init()
    {
        $this->model = new ProjectModel();
    }

    public function queryjoin($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){
        $query = $this->model->join('RIGHT JOIN itboye_project_apply ON itboye_project_apply.project_id = itboye_project.id');

        if(!is_null($map)){
            $query = $query->where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model ->join('RIGHT JOIN itboye_project_apply ON itboye_project_apply.project_id = itboye_project.id')-> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
    }

    public function querythreejoin($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){
        $query = $this->model->join('INNER JOIN itboye_project_apply ON itboye_project_apply.project_id = itboye_project.id')->join('INNER JOIN itboye_ucenter_member ON itboye_project_apply.uid = itboye_ucenter_member.id');

        if(!is_null($map)){
            $query = $query->where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model ->join('INNER JOIN itboye_project_apply ON itboye_project_apply.project_id = itboye_project.id')->join('INNER JOIN itboye_ucenter_member ON itboye_project_apply.uid = itboye_ucenter_member.id')-> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
    }
}